package com.arsn.myf.generate.controller;

import com.arsn.myf.common.util.StringUtils;
import com.arsn.myf.common.util.auth.AuthUtil;
import com.arsn.myf.common.util.result.Func;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.generate.entity.GenerateCodeTable;
import com.arsn.myf.generate.entity.TableColumnInfo;
import com.arsn.myf.generate.service.GenerateCodeService;
import com.arsn.myf.generate.service.TableColumnInfoService;
import com.arsn.myf.generate.vo.GenerateCodeTableVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.wildfly.common.annotation.NotNull;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 代码生成
 * @author zhongrj
 * @since 2022-12-01
 */
@RestController
@RequestMapping("/generateCode")
@AllArgsConstructor
@Api(value = "代码生成", tags = "代码生成")
public class GenerateCodeController {

    private final GenerateCodeService generateCodeService;

    private final TableColumnInfoService tableColumnInfoService;


    /**
     * 查询代码生成分页数据
     * @param page
     * @param generateCodeTable
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询代码生成分页数据")
    public R list(Page<GenerateCodeTable> page, GenerateCodeTable generateCodeTable){
        return R.data(generateCodeService.page(page,new QueryWrapper<>(generateCodeTable)));
    }


    /**
     * 自定义查询代码生成分页数据
     * @param page
     * @param generateCode
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "自定义查询代码生成分页数据")
    public R page(Page<GenerateCodeTableVO> page, GenerateCodeTableVO generateCode){
        return R.data(generateCodeService.selectPageGenerateCodeList(page,generateCode));
    }


    /**
     * 代码生成详情信息
     * @param generateCodeTable
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "代码生成详情信息")
    public R detail(GenerateCodeTable generateCodeTable){
        return R.data(generateCodeService.getOne(new QueryWrapper<>(generateCodeTable)));
    }


    /**
     * 代码生成新增
     * @param generateCodeTable
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "代码生成新增")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody GenerateCodeTable generateCodeTable){
        boolean flag = false;
        //新增
        flag = generateCodeService.save(generateCodeTable);
        //新增表列属性信息
        if (flag){
            flag = saveTableColumnInfo(generateCodeTable);
        }
        return R.status(flag);
    }

    /**
     * 新增表列属性信息
     * @param generateCodeTable
     * @return
     */
    public boolean saveTableColumnInfo(GenerateCodeTable generateCodeTable) {
        //查询表列属性信息
        List<TableColumnInfo> tableColumnInfoList = generateCodeService.getTableColumnInfo(generateCodeTable.getTableName());
        //遍历
        if (tableColumnInfoList.size()>0){
            tableColumnInfoList.forEach(tableColumnInfo -> {
                tableColumnInfo.setTableId(generateCodeTable.getId());
                //java 属性设置
                if (tableColumnInfo.getColumnType().equals("bigint")){
                    tableColumnInfo.setJavaType("Long");
                }else if (tableColumnInfo.getColumnType().equals("varchar") || tableColumnInfo.getColumnType().equals("char") ||
                tableColumnInfo.getColumnType().equals("text")){
                    tableColumnInfo.setJavaType("String");
                }else if (tableColumnInfo.getColumnType().equals("int") || tableColumnInfo.getColumnType().equals("tinyint")){
                    tableColumnInfo.setJavaType("Integer");
                }else if (tableColumnInfo.getColumnType().equals("datetime") || tableColumnInfo.getColumnType().equals("date")){
                    tableColumnInfo.setJavaType("Date");
                }else {
                    tableColumnInfo.setJavaType("String");
                }
                // java 属性名称设置，有下划线的转驼峰
                if (tableColumnInfo.getColumnName().contains("_")){
                    //处理
                    tableColumnInfo.setJavaField(StringUtils.toCamelCase(tableColumnInfo.getColumnName()));
                }else {
                    tableColumnInfo.setJavaField(tableColumnInfo.getColumnName());
                }
                //新增
                tableColumnInfoService.save(tableColumnInfo);
            });
            return true;
        }
        // 返回结果
        return false;
    }

    /**
     * 代码生成修改
     * @param generateCodeTable
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "代码生成修改")
    public R update(@RequestBody GenerateCodeTable generateCodeTable){
        generateCodeTable.setUpdateUser(AuthUtil.getUserId());
        generateCodeTable.setUpdateTime(new Date());
        return R.status(generateCodeService.updateById(generateCodeTable));
    }


    /**
     * 删除,批量删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除,批量删除")
    public R remove(@NotNull String ids){
        return R.status(generateCodeService.removeByIds(Func.stringToLong(Arrays.asList(ids.split(",")))));
    }

    /**
     * 代码生成
     * @param generateCodeTable
     * @return
     */
    @GetMapping("/codeGenerate")
    @ApiOperation(value = "代码生成")
    public void codeGenerate(GenerateCodeTable generateCodeTable, HttpServletResponse response){
        generateCodeService.codeGenerate(generateCodeTable,response);
    }

}
